<?php
/**
 * Created by PhpStorm.
 * User: zxd
 * Date: 2016/12/31 0031
 * Time: 下午 3:27
 */

namespace p5\objects;


class CommonDataParser
{

    static public function getBirthAndGenderByIdCard($idCard)
    {
        $record = false;
        if (!empty($idCard)) {
            if (strlen($idCard) == 18) {
                $record["birthday"] = substr($idCard, 6, 4) . "-" . substr($idCard, 10, 2) . "-" . substr($idCard, 12, 2);
                $record["gender"] = 2 - substr($idCard, 16, 1) % 2 * 1;
            } else if (strlen($idCard) == 15) { //老身份证
                $record["birthday"] = "19" . substr($idCard, 6, 2) . "-" . substr($idCard, 8, 2) . "-" . substr($idCard, 10, 2);
                $record["gender"] = 2 - substr($idCard, 14, 1) % 2 * 1;
            }
        }
        return $record;
    }

    static public function toGenderInt($genderName)
    {
        $genderName = strtolower($genderName);
        $genders = [];
        $genders["男"] = 1;
        $genders["m"] = 1;
        $genders["male"] = 1;
        $genders["女"] = 2;
        $genders["f"] = 2;
        $genders["female"] = 2;
        if (array_key_exists($genderName, $genders)) {
            return $genders[$genderName];
        }
        return 0; // unknow
    }

    static public function toGenderName($genderId)
    {
        $genders = [];
        $genders[1] = "男";
        $genders[2] = "女";
        if (array_key_exists($genderId, $genders)) {
            return $genders[$genderId];
        }
        return "未知"; // unknow
    }
}